Change value of variables between jobs in Gitlab
GitlabVariable scope inside Gitlab-CI is either local to a job or global. At the time of the writing, late 2021, you can not interact with a global variable to change his value from stage to stage. I have read complains about this.
Here is a weird or hugly workaround, call it how you like, but it does the job.
I'm not a Gitlab expert, I might miss something, but here is my need.
- down, process and up are two different runner (tags), it cannot be a before_script of each other.
- If we failed before the down, no need to up after.
- once the down is processed, you do the up after regardless the process result.
How I done in Gitlab.
# .gitlab-ci.yaml
maintenance_down:
stage: maintenance_down
tags:
- gcp-shell
script:
- echo "MAINTENANCE=DOWN" >> build.env
- artisan down
artifacts:
reports:
dotenv: build.env
process:
# some stuff
maintenance_up:
stage: maintenance_up
dependencies:
- maintenance_down
tags:
- gcp-shell
when: always
script:
- echo $MAINTENANCE
- (if [ "$MAINTENANCE" == "DOWN" ]; then artisan up; fi);
Let's explain breifly, you store your variables in a file that will be pass througt jobs as dependencies with the artifacts "reports.dotenv". Told you, hugly hack...
I understand that jobs could be run in differants runner, in parallele, but it would be nice to have some better way to do this.